$(function () {

    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        cart_num: 0,
        dispute_orderinfo: {},
        goods_order_item: [],
        exprress_list:[]

    };
    var app = new Vue({
            el: '#user_goods_order_page',
            data: html_data,
            methods: {
                price_filter: price_filter,
                show_head_img: show_head_img,

                start_goods_order_ready: function () {

                    var parm = {goods_order_id: html_data.orderinfo.id};

                    api.lorder.start_goods_order_ready(parm).then(function (data) {
                        app.get_order();
                    }, function (data) {

                    });
                },
                send_goods_order_express: function () {

                    Magiclamp.mcExpressPrompt("填写快递单", '快递单号', html_data.exprress_list, function (express_id, express_no, express_name) {

                        var parm = {
                            goods_order_id: html_data.orderinfo.id,
                            express_id: express_id,
                            express_no: express_no,
                            express_name: express_name
                        };
                        var b = true;
                        if (express_id == '0') {
                            if (express_name == '') {
                                setTimeout(function () {
                                    Magiclamp.mcAlert("提示", "请填写快递公司名称");
                                }, 100);

                                b = false;
                            }
                        }else{
                            _(html_data.exprress_list).each(function (item) {
                                if(express_id==item.id){
                                    parm['express_name'] = item.name;
                                }
                            });
                        }
                        if (!express_no) {
                            setTimeout(function () {
                                Magiclamp.mcAlert("提示", "请填写快递单号");
                            }, 100);
                            b = false;
                        }
                        if (!b) {
                            return false;
                        }
                        api.lorder.add_goods_order_express(parm).then(function (data) {
                            app.get_order();
                        }, function (data) {

                        });

                    });


                },
                query_express_list: function () {
                    api.lorder.query_express_list().then(function (data) {
                        var exprress_list = data.result.list;
                        exprress_list.push({id: 0, name: "其他"})
                        html_data.exprress_list = exprress_list;
                    });
                },
                get_order: function () {
                    var defer = $.Deferred();
                    api.lorder.get_order_dispute({dispute_id: GetQueryString("dispute_id")}).then(function (data) {

                        html_data.dispute_orderinfo = data.result;
                        if(html_data.dispute_orderinfo.imgs_json){
                            html_data.dispute_orderinfo.imgs_json = JSON.parse(html_data.dispute_orderinfo.imgs_json);
                        }

                        defer.resolve(html_data.goods_order);
                    }, function () {
                        defer.reject();
                    });
                    return defer.promise();
                },

                imageClick:function(bigImgUrl) {
                    Magiclamp.mcImgPop(bigImgUrl);
                },
                open_payorder: function (payorder_id) {
                    location.href = 'pay.html?payorder_id=' + payorder_id;
                },
                shengyudays: function (goods_order) {
                    if (goods_order.goods_status == 4 || goods_order.goods_status == 0 || goods_order.goods_status == 1) {
                        return "";
                    }
                    if (goods_order.dispute_step == 0) {
                        //"received_date" "shipped_date"
                        if (goods_order.goods_status == 2) {
                            var shipped_date = moment();
                            var end_date = moment(goods_order.shipped_date);
                            end_date.add(7, 'days');
                            var days = end_date.diff(shipped_date, 'days');
                            if (days == 0) {
                                return "(剩余：" + end_date.diff(shipped_date, 'hours') + "小时)"
                            } else {
                                return "(剩余：" + days + "天)";
                            }
                        } else if (goods_order.goods_status == 3) {
                            var shipped_date = moment();
                            var end_date = moment(goods_order.received_date);
                            end_date.add(7, 'days');
                            var days = end_date.diff(shipped_date, 'days');
                            if (days == 0) {
                                return "(剩余：" + end_date.diff(shipped_date, 'days') + "小时)"
                            } else {
                                return "(剩余：" + days + "天)";
                            }
                        }

                    } else {
                        return "";
                    }
                },
                delete_payorder: function (payorderid) {
                    Magiclamp.mcConfirm("提示", "确认删除订单么？", null, function () {
                        api.lorder.delete_order({pay_order_id: payorderid}).then(function (data) {
                            Magiclamp.mcAlert("提示", data.message, null, function () {
                                baseConfig.page_back();

                            });

                        });
                    });

                },
                refused_order: function (goods_order) {
                    Magiclamp.mcConfirm("提示", "确认退货么？", null, function () {
                        api.lorder.refund_order_by_user({order_id:goods_order.id}).then(function (data) {
                            app.get_order();
                        })
                    });

                },
                dispute_goods_order: function (goods_order) {

                    location.href = 'user_create_dispute_order.html?goods_order_id=' + goods_order.id;

                    // Magiclamp.mcConfirm("提示", "确认投诉订单么？", null, function () {
                    //     api.lorder.dispute_goods_order({order_id: goods_order.id}).then(function (data) {
                    //         Magiclamp.mcAlert("提示", data.message);
                    //         app.get_order();
                    //     })
                    // });
                },
                dispute_goods_order_item: function (goods_item) {

                    location.href = 'user_create_dispute_order.html?goods_order_item_id=' + goods_item.id;

                    // Magiclamp.mcConfirm("提示", "确认投诉订单么？", null, function () {
                    //     api.lorder.dispute_goods_order({order_id: goods_order.id}).then(function (data) {
                    //         Magiclamp.mcAlert("提示", data.message);
                    //         app.get_order();
                    //     })
                    // });
                },

                make_goods_order: function (goods_order) {
                    api.lorder.confirm_goods_received({goods_order_id: goods_order.id}).then(function (data) {
                        app.get_order();
                        Magiclamp.mcAlert("提示", data.message);
                    });
                },
                close_dispute_order:function () {
                    api.lorder.close_dispute_order_by_user({dispute_id: GetQueryString("dispute_id")}).then(function (data) {

                        html_data.dispute_orderinfo = data.result;
                        if(html_data.dispute_orderinfo.imgs_json){
                            html_data.dispute_orderinfo.imgs_json = JSON.parse(html_data.dispute_orderinfo.imgs_json);
                        }
                    })
                },
                update_goods_order_express:function () {
                    Magiclamp.mcExpressPrompt("填写快递单", '快递单号', html_data.exprress_list, function (express_id, express_no, express_name) {

                        var parm = {
                            express_id: express_id,
                            express_no: express_no,
                            express_name: express_name
                        };
                        var b = true;
                        if (express_id == '0') {
                            if (express_name == '') {
                                setTimeout(function () {
                                    Magiclamp.mcAlert("提示", "请填写快递公司名称");
                                }, 100);

                                b = false;
                            }
                        }else{
                            _(html_data.exprress_list).each(function (item) {
                                if(express_id==item.id){
                                    parm['express_name'] = item.name;
                                }
                            });
                        }
                        if (!express_no) {
                            setTimeout(function () {
                                Magiclamp.mcAlert("提示", "请填写快递单号");
                            }, 100);
                            b = false;
                        }
                        if (!b) {
                            return false;
                        }
                        parm['dispute_id'] = GetQueryString("dispute_id");
                        api.lorder.update_express_info_order_dispute_by_user(parm).then(function (data) {
                            html_data.dispute_orderinfo = data.result;
                            if(html_data.dispute_orderinfo.imgs_json){
                                html_data.dispute_orderinfo.imgs_json = JSON.parse(html_data.dispute_orderinfo.imgs_json);
                            }
                        });

                    });
                },

                appley_sys_dispute_order:function () {
                    Magiclamp.mcPrompt("申请平台介入","请输入原因", function (message) {
                        api.lorder.apply_dispute_order_sys_by_user({dispute_id: GetQueryString("dispute_id"), message:message}).then(function (data) {

                            html_data.dispute_orderinfo = data.result;
                            if(html_data.dispute_orderinfo.imgs_json){
                                html_data.dispute_orderinfo.imgs_json = JSON.parse(html_data.dispute_orderinfo.imgs_json);
                            }
                        })
                    })
                },
                open_order:function (dispute_order) {
                    window.location.href='./user_goods_orders.html?goods_order_id='+dispute_order.order_id
                },

                open_im_2_user: function (orderinfo) {
                    client_storage("page", "tab_chat");
                    client_storage("current_imusername", orderinfo.order__host__imusername);
                    client_storage("current_realname", orderinfo.order__host__realname);
                    if(orderinfo.order__goodsstore_id){
                        client_storage("current_realname", orderinfo.order__goodsstore__name);
                        client_storage("goodsstore_id", orderinfo.order__goodsstore_id);
                    }else{
                        client_storage("current_realname", orderinfo.order__host__realname);
                        client_storage("goodsstore_id", 0);
                    }
                    window.location.href="home.html"

                },
                close_page: function () {
                    ttjd.close_phonegap();
                },
                page_back: function () {
                    baseConfig.page_back();
                }
            }
        })
        ;

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }


    app.get_order();
    app.query_express_list();

// api.sys.my_userinfo()


//
// $("#get-code-btn").on('click',function () {
//     if($(".addres-list").css('display')!=='none'){
//         $('#upOrdown').removeClass('i-up').addClass('i-down');
//         $(".addres-list").slideUp();
//     }else{
//         $(".addres-list").slideDown();
//         $('#upOrdown').removeClass('i-down').addClass('i-up');
//     }
// });

// $('#tab-bar').remove();
// $('#pay_room').on('keydown', function (event) {
//     //禁止回车键提交form
//     if (event.keyCode === 13) {
//         return false;
//     }
// });
// $('.question').on('click', function () {
//     Magiclamp.mcAlert('关于服务费', '如果您的订单中含有前台送货商品,我们将收取一定金额的服务费')
// });

// 显示金额统计判断
// money_count();
// function money_count() {
//     var total_price = $("#total_price").html() * 100;
//     var pass_price = 0;
//     var vmoney = $("#vmoney").html() * 100;
//     var acanuse = $("#acanuse").html() * 100;
//     if ($("#acanuse_check").is(":checked")) {
//         total_price = total_price - acanuse;
//         pass_price = pass_price + acanuse;
//     }
//     if ($("#vmoney_check").is(":checked")) {
//         total_price = total_price - vmoney;
//         pass_price = pass_price + vmoney;
//     }
//     total_price = (Math.max(total_price, 0)) / 100;
//     pass_price = (Math.min(pass_price, $("#total_price").html() * 100)) / 100;
//     $("#pay_price").html(total_price.toFixed(2));
//     $("#pass_price").html(pass_price.toFixed(2));
// }

// $("#acanuse_check").on('click', function () {
//     if ($("#vmoney_check").is(":checked") && $("#vmoney").html() * 100 >= $("#total_price").html() * 100) {
//         Magiclamp.mcAlert('', '您已选择的赠款足够支付该订单！');
//         $(this).prop('checked', false);
//     }
//     money_count();
// });
// $("#vmoney_check").on('click', function () {
//     if ($("#acanuse_check").is(":checked") && $("#acanuse").html() * 100 >= $("#total_price").html() * 100) {
//         Magiclamp.mcAlert('', '您已选择的余额足够支付该订单！');
//         $(this).prop('checked', false);
//     }
//     money_count();
// });
// $(".paycheckbox").on('click', function () {
//     var usermoney = 0;
//     if ($("#acanuse_check").is(":checked")) {
//         usermoney += $("#acanuse").html() * 100;
//     }
//     if ($("#vmoney_check").is(":checked")) {
//         usermoney += $("#vmoney").html() * 100;
//     }
//     if (usermoney > $("#total_price").html() * 100) {
//         Magiclamp.mcAlert('', '您已选择的余额或者赠款足够支付该订单！');
//         $(this).prop('checked', false);
//     }
// });
// 显示金额统计判断 end


// $("#gotopay_form").on('submit', function () {
//     return false;
// });//禁止表单直接提交

// $("#gotopay").on('click', function () {
//     $(this).css('pointer-events', 'none');
//     var url = $("#gotopay_form").attr('action');
//     var data = $("#gotopay_form").serializeArray();
//     Magiclamp.midLoadingShow('处理中...');
//     Magiclamp.mcAjax(url, 'POST', data, function (data) {
//             $("#gotopay").css('pointer-events', 'auto');
//             if (parseInt(data.status) === 1) {
//                 Magiclamp.midLoadingHide();
//                 location.href = data.data.payurl;
//             } else {
//                 if (data.message.length > 0) {
//                     Magiclamp.midLoadingHide();
//                     Magiclamp.mcAlert('', data.message);
//                 } else {
//                     Magiclamp.midLoadingHide();
//                     Magiclamp.mcAlert('', '支付失败');
//                 }
//             }
//         },
//         function () {
//             $("#gotopay").css('pointer-events', 'auto');
//         }
//     );
//     return false;
// });
})
;
